package com.github.shiqiyue.blog.markdown.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.shiqiyue.blog.markdown.controller.base.BaseController;
import com.github.shiqiyue.blog.markdown.entity.Article;
import com.github.shiqiyue.blog.markdown.entity.User;
import com.github.shiqiyue.blog.markdown.service.ArticleService;
import com.github.shiqiyue.blog.markdown.utils.Assert;
import com.github.shiqiyue.blog.markdown.vo.rep.base.RepBaseVO;
import com.youbenzi.mdtool.tool.MDTool;

/***
 * 文章 <br/>
 * 访问权限：默认登录后才能访问
 * 
 * @author wwy
 *
 */
@PreAuthorize("hasRole('USER')")
@RequestMapping("/article")
@Controller
public class ArticleController extends BaseController {
	
	@Autowired
	private ArticleService articleService;
	
	private static final String ARTICLE_NOT_EXIST = "文章不存在";
	
	@GetMapping("add")
	public String addArticlePage(Model model) {
		return "article/add";
	}
	
	@GetMapping("edit")
	public String editArticlePage(Long id, @RequestParam(name = "redirectUrl", defaultValue = "/") String redirectUrl,
			Model model) {
		User user = getUser();
		Article article = articleService.findById(id);
		Assert.notNull(article, ARTICLE_NOT_EXIST);
		Assert.equal(article.getUser().getId(), user.getId(), ARTICLE_NOT_EXIST);
		model.addAttribute("article", article);
		model.addAttribute("redirectUrl", redirectUrl);
		return "article/edit";
	}
	
	@PostMapping("add")
	@ResponseBody
	public RepBaseVO addArticle(Article article) {
		RepBaseVO repVO = new RepBaseVO<>();
		article.setUser(getUser());
		article.setAddDate(new Date());
		article.setEditDate(new Date());
		articleService.save(article);
		return repVO.setCommonSuccess();
	}
	
	@PostMapping("edit")
	@ResponseBody
	public RepBaseVO editArticle(Article article) {
		RepBaseVO repVO = new RepBaseVO<>();
		Article oldArticle = articleService.findById(article.getId());
		Assert.notNull(oldArticle, ARTICLE_NOT_EXIST);
		Assert.equal(oldArticle.getUser().getId(), getUser().getId(), ARTICLE_NOT_EXIST);
		oldArticle.setBrief(article.getBrief());
		oldArticle.setContent(article.getContent());
		oldArticle.setTitle(article.getTitle());
		oldArticle.setEditDate(new Date());
		articleService.save(oldArticle);
		return repVO.setCommonSuccess();
	}
	
	@GetMapping("list")
	public String articleListPage(@RequestParam(name = "page", defaultValue = "0") int page,
			@RequestParam(name = "size", defaultValue = "10") int size, Model model) {
		Article example = new Article();
		example.setUser(getUser());
		Page<Article> articlePage = articleService
				.page(new PageRequest(page, size, new Sort(Direction.DESC, "addDate")), Example.of(example));
		model.addAttribute("articlePage", articlePage);
		return "article/list";
	}
	
	@PostMapping("del")
	@ResponseBody
	public RepBaseVO delArticle(Long id) {
		Article article = articleService.findById(id);
		Assert.equal(article.getUser().getId(), getUser().getId(), ARTICLE_NOT_EXIST);
		articleService.delete(article);
		return RepBaseVO.newIntance().setCommonSuccess();
	}
	
	/***
	 * 文章详情 <br/>
	 * 访问权限：任意
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@PreAuthorize("permitAll()")
	@GetMapping("detail")
	public String articleDetailPage(Long id, Model model) {
		Article article = articleService.findById(id);
		article.setContent(MDTool.markdown2Html(article.getContent()));
		model.addAttribute("article", article);
		return "article/detail";
	}
	
}
